<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT, Blog, JAVA,Spring,Mysql,Quartz,Python,程序员" />





  <link rel="alternate" href="https://my.oschina.net/giegie/rss" title="Gie的博客" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="编程约束（一）命名约束

【强制】所有编程相关命名均不能以下划线和美元符号开始，也不能以下划线和美元符号介绍。反例： name / $name / name / name$ 
【强制】所有编程相关命名严禁使用英文与拼音混合的方式，更不能允许直接使用中文的方式。说明： 正确的英文拼写和语法可以让阅读者易于理解，避免歧义。也要尽量避免拼音命名反例： DaZhePromotion [打折] / getP">
<meta property="og:type" content="article">
<meta property="og:title" content="JAVA命名规约">
<meta property="og:url" content="https://jieblog.github.io/2017/02/07/命名规约/index.html">
<meta property="og:site_name" content="Gie的博客">
<meta property="og:description" content="编程约束（一）命名约束

【强制】所有编程相关命名均不能以下划线和美元符号开始，也不能以下划线和美元符号介绍。反例： name / $name / name / name$ 
【强制】所有编程相关命名严禁使用英文与拼音混合的方式，更不能允许直接使用中文的方式。说明： 正确的英文拼写和语法可以让阅读者易于理解，避免歧义。也要尽量避免拼音命名反例： DaZhePromotion [打折] / getP">
<meta property="og:updated_time" content="2017-02-23T06:12:09.316Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JAVA命名规约">
<meta name="twitter:description" content="编程约束（一）命名约束

【强制】所有编程相关命名均不能以下划线和美元符号开始，也不能以下划线和美元符号介绍。反例： name / $name / name / name$ 
【强制】所有编程相关命名严禁使用英文与拼音混合的方式，更不能允许直接使用中文的方式。说明： 正确的英文拼写和语法可以让阅读者易于理解，避免歧义。也要尽量避免拼音命名反例： DaZhePromotion [打折] / getP">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '75HUY04HZY',
      apiKey: 'c215d1b4c97f100bad332f84e968d70a',
      indexName: 'qq154987607',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://jieblog.github.io/2017/02/07/命名规约/"/>





  <meta name="baidu-site-verification" content="cKXv4cbRZc" />
  <meta name="google-site-verification" content="05Vxc1yd7lC2F1bxwmmbQkfP-rZgvPLTqufHSzImPcM" />
  <title> JAVA命名规约 | Gie的博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7e8381cb6c39db6048492ff2e52234cb";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Gie的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">为人瓜而不皮 一身正气</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="https://jieblog.github.io/2017/02/07/命名规约/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Gie">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/logo.png">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Gie的博客">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Gie的博客" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                JAVA命名规约
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-02-07T00:00:00+08:00">
                2017-02-07
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JAVA/" itemprop="url" rel="index">
                    <span itemprop="name">JAVA</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="编程约束"><a href="#编程约束" class="headerlink" title="编程约束"></a>编程约束</h1><h2 id="（一）命名约束"><a href="#（一）命名约束" class="headerlink" title="（一）命名约束"></a>（一）命名约束</h2><hr>
<ol>
<li>【强制】所有编程相关命名均不能以下划线和美元符号开始，也不能以下划线和美元符号介绍。<br><strong>反例：</strong> <em>name / $name / name</em> / name$ </li>
<li>【强制】所有编程相关命名严禁使用英文与拼音混合的方式，更不能允许直接使用中文的方式。<br><strong>说明：</strong> 正确的英文拼写和语法可以让阅读者易于理解，避免歧义。也要尽量避免拼音命名<br><strong>反例：</strong> DaZhePromotion [打折] / getPingfenByName() [评分] / int 变量 = 3;<br><strong>正例：</strong> ali / alibaba / taobao / cainiao / aliyun / youku / hangzhou 等国际通用的名称，可视为英文。</li>
<li>【强制】类名使用UpperCamelCase风格，必须遵从驼峰命名形式，但以下情况例外：（领域模型相关）DO / DTO / VO / DAO 等<br><strong>正例:</strong> MarcoPolo / UserDO / XmlService / TcpUdpDeal / TaPromotion<br><strong>反例:</strong> macroPolo / UserDo / XMLService / TCPUDPDeal / TAPromotion</li>
<li>【强制】方法名、参数名、成员变量、局部变量都统一使用lowerCamelCase风格，必须遵从驼峰命名形式。<br><strong>正例：</strong> localValue / getHttpMessage() / inputUserId  </li>
<li>【强制】常量命名全部大写，单词间用下划线隔开，力求语义清晰不要嫌命名过长<br><strong>正例:</strong> MAX_STOCK_COUNT<br><strong>反例:</strong> MAX_COUNT</li>
<li>【强制】抽象类命名使用Abstract或Base开头；异常类使用Exception结尾；测试类命名以它要测试的类开头以Test结尾。</li>
<li>【强制】中括号是数组类型的一部分，数组定义如下：String[] args<br><strong>正例:</strong> String args[]来定义</li>
<li>【强制】POJO类型中的任何布尔型变量不要使用is开头，否则部分框架可能引起解析错误<br><strong>反例:</strong> 定义为基本数据类型 boolean isSuccess；的属性，它的方法也是 isSuccess()，RPC<br>框架在反向解析的时候，“以为”对应的属性名称是 success，导致属性获取不到，进而抛出<br>异常。</li>
<li>【强制】包名统一使用小写，点分隔符之间有且只有一个自然语义的单词。包名统一是用单数形式。<br><strong>正例:</strong> 应用工具类包名为 com.alibaba.mpp.util、类名为 MessageUtils（此规则参考 spring<br>的框架结构）</li>
<li>【强制】杜绝完全不规范的缩写，避免忘文不知义。<br><strong>正例:</strong> &lt;某业务代码&gt;AbstractClass“缩写”命名成 AbsClass；condition“缩写”命名成<br>condi，此类随意缩写严重降低了代码的可阅读性。</li>
<li><p>【推荐】 如果使用到设计模式，建议在类名中体现出具体的模式。<br><strong>说明:</strong> 将设计模式体现在命名中，有利于阅读者快速理解架构设计思想。<br><strong>正例:</strong> </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">public class OrderFactory;</div><div class="line">public class LoginProxy;</div><div class="line">public class ResourceObserver;</div></pre></td></tr></table></figure>
</li>
<li><p>【推荐】 接口类的方法和属性不要加任何修饰符号（public 也不要加），保持代码的简洁性，并加上有效的javadoc 注释。尽量不要在接口中定义变量，如果一定要定义变量，肯定是于接口方法有关，并且是整个应用的基础常量。<br><strong>正例:</strong> 接口方法签名：void f();  接口基础常量表示：String COMPANY = “alibaba”;<br><strong>反例:</strong>  接口方法定义：public abstract void f();<br><strong>说明:</strong> JDK8 中接口允许有默认实现，那么这个 default 方法，是对所有实现类都有价值的默<br>认实现。</p>
</li>
<li><p>接口和实现类的两套规则</p>
</li>
</ol>
<ul>
<li>【强制】对于 Service 和 DAO 类，基于 SOA 的理念，暴露出来的服务一定是接口，内部<br>的实现类用 Impl 的后缀与接口区别。<br><strong>正例:</strong> CacheServiceImpl 实现 CacheService 接口。</li>
<li>【推荐】 如果是形容能力的接口名称，取对应的形容词做接口名（通常是–able 的形<br>式）。<br><strong>正例:</strong> AbstractTranslator 实现 Translatable。</li>
</ul>
<ol>
<li>【参考】枚举类名建议带上 Enum 后缀，枚举成员名称需要全大写，单词间用下划线隔开。<br><strong>说明:</strong> 枚举类其实就是特殊的常量类，而且默认构造方法默认私有 （单例）。<br><strong>正例:</strong> 枚举名字：DealStatusEnum；成员名称：SUCCESS / UNKOWN_REASON。</li>
<li>【参考】各层命名规约</li>
</ol>
<ul>
<li>Service/DAO 层方法命名规约</li>
<li><ul>
<li>获取单个对象的方法用 get 做前缀。</li>
</ul>
</li>
<li><ul>
<li>获取多个对象的方法用 list 做前缀。</li>
</ul>
</li>
<li><ul>
<li>获取统计值的方法用 count 做前缀。</li>
</ul>
</li>
<li><ul>
<li>插入的方法用 save（推荐）或 insert 做前缀。</li>
</ul>
</li>
<li><ul>
<li>删除的方法用 remove（推荐）或 delete 做前缀。</li>
</ul>
</li>
<li><ul>
<li>修改的方法用 update 做前缀。</li>
</ul>
</li>
<li>领域模型命名规约</li>
<li><ul>
<li>数据对象：xxxDO，xxx 即为数据表名。</li>
</ul>
</li>
<li><ul>
<li>数据传输对象：xxxDTO，xxx 为业务领域相关的名称。</li>
</ul>
</li>
<li><ul>
<li>展示对象：xxxVO，xxx 一般为网页名称。</li>
</ul>
</li>
<li><ul>
<li>POJO 是 DO/DTO/BO/VO 的统称，禁止命名成 xxxPOJO。</li>
</ul>
</li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/01/29/【nginx启动】 97 Address family not supported by protocol/" rel="next" title="【nginx启动】 97 Address family not supported by protocol">
                <i class="fa fa-chevron-left"></i> 【nginx启动】 97 Address family not supported by protocol
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/02/12/MySQL二进制日志/" rel="prev" title="MySQL二进制日志">
                MySQL二进制日志 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8yNzgyMS80Mzk3"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/logo.png"
               alt="Gie" />
          <p class="site-author-name" itemprop="name">Gie</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">94</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
        </nav>

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/jieblog" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://git.oschina.net/log4j" target="_blank" title="GitOsc">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  GitOsc
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/3911368554" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="wangiegieg@gmail.com" target="_blank" title="Email">
                  
                    <i class="fa fa-fw fa-google"></i>
                  
                  Email
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#编程约束"><span class="nav-number">1.</span> <span class="nav-text">编程约束</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#（一）命名约束"><span class="nav-number">1.1.</span> <span class="nav-text">（一）命名约束</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Gie</span>
</div>



        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
    <span class="site-pv">本站总访问量<span class="busuanzi-value" id="busuanzi_value_site_pv"></span>次</span>
  

</div>



        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.0"></script>



  

</body>
</html>
